package com.siyu.preplan.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.siyu.basedata.plan.domain.Plan;
import com.siyu.basedata.plan.service.IPlanService;
import com.siyu.common.config.minio.MinIOConfigProperties;
import com.siyu.common.core.controller.BaseController;
import com.siyu.common.core.domain.AjaxResult;
import com.siyu.common.utils.minio.service.FileStorageService;
import com.siyu.predict.dto.PlanDto;
import com.siyu.preplan.dto.PlanListDto;
import com.siyu.preplan.dto.PlanPDepotDto;
import com.siyu.preplan.planpcondition.domain.PlanPCondition;
import com.siyu.preplan.planpcondition.service.IPlanPConditionService;
import com.siyu.preplan.planpcondition.service.impl.PlanPConditionServiceImpl;
import com.siyu.preplan.planpdepot.domain.PlanPDepot;
import com.siyu.preplan.planpdepot.service.IPlanPDepotService;
import com.siyu.preplan.planpdocument.domain.PlanPDocument;
import com.siyu.preplan.planpdocument.service.IPlanPDocumentService;
import com.siyu.preplan.planpdocument.service.impl.PlanPDocumentServiceImpl;
import com.siyu.preplan.planpdtml.domain.PlanPDtml;
import com.siyu.preplan.planpmaterial.domain.PlanPMaterial;
import com.siyu.preplan.planpmaterial.service.IPlanPMaterialService;
import com.siyu.preplan.planpmaterial.service.impl.PlanPMaterialServiceImpl;
import com.siyu.preplan.planpplan.domain.PlanPPlan;
import com.siyu.preplan.planpplan.service.impl.PlanPPlanServiceImpl;
import com.siyu.preplan.service.PlanPeService;
import com.siyu.preplan.service.impl.PlanPeServiceImpl;
import com.siyu.preplan.vo.PlanPMaterialVo;
import com.siyu.preplan.vo.PlanPPlanVo;
import com.siyu.preplan.vo.PlanSummaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/preplan/emergency")
public class PlanPeController extends BaseController {

    @Autowired
    private IPlanService planService;

    @Autowired
    private IPlanPMaterialService planPMaterialService;


    @Autowired
    private PlanPeService planPeService;

    @Autowired
    private IPlanPDepotService planPDepotService;


    /**
     * 预案列表查询
     *
     * @param
     * @return
     */
    @GetMapping("/plan/list")
    public AjaxResult getPlanList(@RequestParam("rscd") String rscd) {
        QueryWrapper<Plan> planQueryWrapper = new QueryWrapper<>();
        planQueryWrapper.select("id", "create_by", "plan_name", "step", "create_time", "plan_desc")
                /*  .like(BeanUtil.isNotEmpty(planDto.getPlanName()),"plan_name",planDto.getPlanName())*/
                .eq("pre_type", "预案")
                .eq("rscd", rscd)
                .orderByDesc("create_time");
        return AjaxResult.success(planService.list(planQueryWrapper));
    }

    ;

    /**
     * 预案信息查询
     *
     * @param id
     * @return
     */
    @GetMapping("/plan/information")
    public AjaxResult getplanPMaterial(@RequestParam("id") String id) {
        QueryWrapper<PlanPMaterial> planPMaterialQueryWrapper = new QueryWrapper<>();
        planPMaterialQueryWrapper.select("rescue_tools", "basic_materials", "set_lights", "correspondence", "first_aid", "transport")
                .eq("plan_id", id);
        return AjaxResult.success(planPMaterialService.getOne(planPMaterialQueryWrapper));
    }

    ;


    /**
     * 预案删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/plan/delete")
    @Transactional
    public AjaxResult delete(@RequestBody List<String> ids) {
        try {
            planPeService.delete(ids);
        } catch (Exception e) {
            return AjaxResult.error("删除失败!");
        }

        return AjaxResult.success("删除成功!");
    }

    ;

    /**
     * 预案信息查询
     *
     * @param id
     * @return
     */
    @GetMapping("/plan/respond")
    public AjaxResult getPlanPPlanVos(@RequestParam("id") String id,
                                      @RequestParam("planLevel") Integer planLevel) {
        PlanPPlanVo planPPlanVo = planPeService.getPlanPPlanVos(id, planLevel);
        if (planPPlanVo == null) {
            return AjaxResult.warn("查无数据!");
        }
        return AjaxResult.success(planPPlanVo);
    }

    ;


    /**
     * 救援物资查询
     *
     * @param id
     * @return
     */
    @GetMapping("/plan/visualization")
    public AjaxResult get(@RequestParam("id") String id, @RequestParam("planLevel") Integer planLevel) {
        PlanPMaterialVo planPMaterialVo = planPeService.getPlanPMaterialVo(id, planLevel);
        return AjaxResult.success(planPMaterialVo);
    }

    ;

    /**
     * 对象概述
     *
     * @param rscd
     * @return
     */
    @GetMapping("/plan/summary")
    public AjaxResult getPlanSummaryVo(@RequestParam("rscd") String rscd) {
        PlanSummaryVo planSummaryVo = planPeService.getPlanSummaryVo(rscd);
        return AjaxResult.success(planSummaryVo);
    }


    /**
     * 预案可视化
     * @param id
     * @param planLevel
     * @return
     */
    @GetMapping("/plan/view")
    public AjaxResult getView(@RequestParam("id")String id,
                              @RequestParam("planLevel")Integer planLevel){
        return planPeService.getView(id,planLevel);
    }

    /**
     * 仓库基础信息查询
     * @param id
     * @return
     */
    @GetMapping("/plan/depot")
    public AjaxResult getDepot(@RequestParam("id")String id){
        return planPeService.getDepot(id);
    }


    /**
     * 仓库物资列表查询
     * @param rscd
     * @return
     */
    @GetMapping("/plan/listdepot")
    public AjaxResult getListdepot(@RequestParam("rscd") String rscd) {
        return planPeService.getListdepot(rscd);
    }


    /**
     * 修改预案仓库基础信息和物资信息
     * @param planPDepotDtos
     * @return
     */
    @PostMapping("/plan/depotedit")
    public AjaxResult edit(@RequestBody List<PlanPDepotDto> planPDepotDtos)
    {
        try {
            planPeService.edit(planPDepotDtos);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败!");
        }
        return AjaxResult.success();
    }

    /**
     * 修改物资信息
     * @param planPDtmls
     * @return
     */
    @PutMapping("/plan/materialedit")
    public AjaxResult materialedit(@RequestBody List<PlanPDtml> planPDtmls)
    {
        try {
            planPeService.materialedit(planPDtmls);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败!");
        }
        return AjaxResult.success();
    }

    /**
     * 删除仓库基本信息
     * @param ids
     * @return
     */
    @DeleteMapping("/plan/deletedepot")
    public AjaxResult deletedepot(@RequestBody List<String> ids) {
        return planPeService.deletedepot(ids);
    }

    /**
     * 删除物资信息
     * @param ids
     * @return
     */
    @DeleteMapping("/plan/deletematerial")
    public AjaxResult deletematerial(@RequestBody List<String> ids) {
        return planPeService.deletematerial(ids);
    }

}
